#include "dgramdomainsocket.hpp"
#include <iostream>

using namespace std;

int main()
{
    // Our constants:
    constexpr const char* path = "./localhostudp";

    constexpr const char* path2 = "./localhostudp22";


    // Initialize socket.
    DgramDomainSocket<100> udpSocket(true); // "true" to use Connection on UDP. Default is "false".
    


    // Bind the server to a port.
    udpSocket.Bind(path2, [](int errorCode, string errorMessage) {
    // BINDING FAILED:
        cout << errorCode << " : " << errorMessage << endl;
    });
    
    udpSocket.Connect(path);

    // Send String:
    udpSocket.Send("ABCDEFGH");
    //udpSocket.SendTo("ABCDEFGH", IP, PORT); // If you want to connectless

    udpSocket.onRawMessageReceived = [&](const char* message, int length, string paths) {
        cout << paths << " => " << message << "(" << length << ")" << endl;
    };
    
    // If you want to use std::string:
    /*
    udpSocket.onMessageReceived = [&](string message, string ipv4, uint16_t port) {
        cout << ipv4 << ":" << port << " => " << message << endl;
    };
    */

    // You should do an input loop, so the program won't terminate immediately
    string input;
    getline(cin, input);
    while (input != "exit")
    {
        udpSocket.Send(input);
        getline(cin, input);
    }

    // Close the socket.
    udpSocket.Close();

    return 0;
}
